<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate() {
                    try{
                        $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                        $query="SELECT a.password, a.name FROM os_admin a 
                            WHERE a.username='{$this->username}' limit 1 ";
                        $command=$connection->createCommand($query);
                        $dataReader=$command->query();
                        $rows_user = array();
                        while(($row=$dataReader->read())!==false) {
                            $rows_user[]=$row;
                        }
                        if(empty($rows_user)) 
                             $this->errorCode=self::ERROR_USERNAME_INVALID;
                        elseif($rows_user[0]["password"]!==$this->password)
                                $this->errorCode=self::ERROR_PASSWORD_INVALID;
                        else {
                                $this->errorCode=self::ERROR_NONE;
                                //save name to the session['name'];
                                Yii::app()->session['name'] = $rows_user[0]["name"]; 
                    }
                        return !$this->errorCode;
                    }catch(Exception $e){}     
                    }
                    
}